package com.GamersCom.servlet;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.GamersCom.db.User;
import com.GamersCom.model.MgrFactory;
import com.GamersCom.model.UserMgr;

/**
 * Servlet implementation class RetrievePassword
 */
public class RetrievePassword extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String nextView = "retrievepassword.jsp";
		String username = request.getParameter("username");
		
		System.out.println("username :" + username);
		
		// input 
		if ( username != null )
		{
			MgrFactory factory = (MgrFactory) this.getServletContext()
					.getAttribute("mgrFactory");
			UserMgr userMgr = factory.getUserMgr();
			//retrieve the user
			User user = userMgr.getUserByUsername(username);
			if (user == null) // user is not found
			{
				request.setAttribute("msg", "User does not exits!");
			}
			else
			{
				request.setAttribute("user", username);
				// set the username into request attribute
				request.setAttribute("securityquestion", user.getSecurityQuestion());
			}
		}	
		
		request.getRequestDispatcher(nextView).forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String nextView = "/retrievepassword.jsp";
		String username = request.getParameter("username");
		String answer = request.getParameter("answer");
		String submit = request.getParameter("submit");
System.out.println("username => "+ username);

		MgrFactory factory = (MgrFactory) this.getServletContext()
				.getAttribute("mgrFactory");
		UserMgr userMgr = factory.getUserMgr();
		// retrieve the user
		// User user = userMgr.getUserByEmail(email);
		User user = userMgr.getUserByUsername(username);

			

			// respond to retrieve password submission
			if (submit.equals("Retrieve Password"))
			{
				if (!userMgr.isValidAnswer(user, answer)) // not valid security
															// answer
					request.setAttribute("msg", "Wrong security answer!");
				else
				{
					request.setAttribute("user", username);
					nextView = "setNewPasswordForm.jsp";
				}
			}

			// respond to set new passwor submission
			if (submit.equals("Set New Password"))
			{
				// get the new password
				String password = request.getParameter("password");

				// update User record
				user.setPassword(password);
				userMgr.updateUser(user);

				request.removeAttribute("username");
				request.setAttribute("msg",
						"Your password has been updated successfully!");
			}
		

		request.getRequestDispatcher(nextView).forward(request, response);
	}

	
}
